{extend name="Public:content"}
{block name="button"}
<!--工具栏 start-->
<div class="row">
    <div class="col-sm-12 m-b-xs">
        <button class="btn btn-white btn-sm" type="button"  onClick="$.Oa.reload()"><i class="fa fa-refresh"></i> 刷新</button>
        <a class="btn btn-primary btn-sm"  title="添加标签" onclick="tag_index.add()" ><i class="fa fa-plus"></i> 添加标签</a>
    </div>
</div>
<!--工具栏 end-->
{/block}
<!--搜索栏 start-->
{block name="search"}
<form id="tag_search_form">
    <div class="row">
        <div class="col-sm-3 m-b-xs">
            <div class="input-group m-b"><span class="input-group-addon">标签名称</span>
                <input name="tag" placeholder="请输入标签名称" class="form-control">
            </div>
        </div>
        <div class="col-sm-2 m-b-xs">
            <div class="input-group m-b"><span class="input-group-addon">是否推荐</span>
                <select name='recommend' class='form-control m-b'>
                    <option value=''> 所有 </option>
                    <option value='1'>推荐</option>
                    <option value='2'>不推荐</option>
                </select>
            </div>
        </div>
        <div class="col-sm-3 m-b-xs">
            <div class="input-group m-b"><span class="input-group-addon">所属分类</span>
                <select name='catid' class='form-control m-b'>
                    <option value=''> 所有 </option>
                    {foreach $cates as $cate}
                    {eq name='cate.id' value='$vo.pid|default=0'}
                    <option value='{$cate.id}' {eq name="cate.id" value="$get_catid|default=0"}selected{/eq}>{$cate.spl}{$cate.cate}</option>
                    {else}
                    <option value='{$cate.id}' {eq name="cate.id" value="$get_catid|default=0"}selected{/eq}>{$cate.spl}{$cate.cate}</option>
                    {/eq}
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="col-sm-6 m-b-xs">
            <div class="input-group m-b"><span class="input-group-addon">来源</span>
                <select name='from' class='form-control m-b' style="width: 30%">
                    <option value=''> 所有 </option>
                    <option value='0'>系统</option>
                    {foreach $users as $user}
                    <option value="{$user.id}">{$user.nickname}</option>
                    {/foreach}
                </select>
                <button type="button" class="btn btn-info sub" style="margin-bottom: 0px; margin-left: 10px;" onclick="tag_index.search()">搜索</button>
            </div>
        </div>
    </div>
</form>
{/block}
<!--搜索栏 end-->
{block name="content"}
<div class="table-responsive" style="overflow-x:initial">
    <table id="tag_table" ></table>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    window.tag_index = {
        //定义工具栏
        //toolbar: "#tag_index_toolbar",
        search: function(){ //搜索
            var query_params = {};
            $.each($("#tag_search_form").serializeArray(),function(){
                query_params[$(this).attr('name')] = $(this).attr('value');
            });
            $('#tag_table').bootstrapTable('refreshOptions',{
                pageNumber : 1,
                queryParams:function(params){
                    var tmp_params = {limit:params.pageSize,page:params.pageNumber,order:params.sortOrder};
                    var query = $.extend(true,tmp_params,query_params);
                    return query;
                }
            });
        },
        add: function(){ //添加
            parent.layer.open({
                type: 2,
                title: '{$title} > 添加',
                shadeClose: false,
                shade: 0.8,
                maxmin: false, //开启最大化最小化按钮
                area: ['960px', '650px'],
                content: '{:url("$classuri/add")}?iframe='+window.name,
                yes: function (index, layero){},
                end: function (){}
            });
        },
        del: function(_this,id){
            if(!id){
                layer.msg('请选择需要删除的数据');
                return false;
            }
            $.Oa.delete(_this,function(data){
                //删除该行
                $("#tag_table").bootstrapTable('remove',{field:'id',values:[id]});
            })
        },
        //操作格式化
        opert: function(value,row){
            var rst = '';
            var del_auth = '{:auth("$classuri/del")}';
            if(del_auth) {
                rst +=  '<a onClick="tag_index.del(this,'+row.id+')" data-href="{:url(\"$classuri/del\")}" data-id="'+row.id+'" class="btn btn-outline btn-xs btn-danger" title="删除" >删除</a>';
            }
            return rst;
        }
    }
    $('#tag_table').bootstrapTable({
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        url:"{:url(\"$classuri/index\")}",
        pageNumber: 1, //初始化加载第一页，默认第一页
        showRefresh: false,
        pagination:true,//是否分页
        sortOrder: 'desc',
        sidePagination:'server',//指定服务器端分页
        queryParamsType : "",
        queryParams: function(params){
            return {limit:params.pageSize,page:params.pageNumber,order:params.sortOrder};
        },//参数
        pageSize:10,//单页记录数
        pageList:[10,20,30,40],//分页步进值
        // responseHandler:responseHandler,//请求数据成功后，渲染表格前的方法
        columns :[
            {field:'id',title:'ID',width: 20},
            {field:'sort',title:'排序',width: 20,formatter:function(value,row){return '<a id="sort" data-pk="'+row.id+'" data-id="'+row.id+'">'+value+'</a>'}},
            {field:'tag',title:'标签名称',width: 150,formatter:function(value,row){return '<a id="tag" data-pk="'+row.id+'" data-id="'+row.id+'">'+value+'</a>'}},
            {field:'catid',title:'所属分类',width: 150},
            {field:'from',title:'来源',width: 150},
            {field:'recommend',title:'是否推荐',align:'center',width: 150,formatter:function(value,row){
                var str = value == 1 ? '推荐':'不推荐';
                return '<button class="btn btn-primary btn-xs" id="recommend" data-pk="'+row.id+'" data-id="'+row.id+'">'+str+'</button>'
            }},
            {field:'action',title:'操作',width: 100,formatter:tag_index.opert},
        ],
        onLoadSuccess: function(data){
            $.each(['#sort','#tag'],function(i,ob){
                $("#tag_table "+ob).editable({
                    url: function (params){$.Oa.updateField('{:url("$classuri/edit")}',params.pk,params.name,params.value)},
                    type: 'text'
                });
            });
            $("#tag_table #recommend").editable({
                url: function (params){$.Oa.updateField('{:url("$classuri/edit")}',params.pk,params.name,params.value)},
                source: {1:'推荐',2:'不推荐'},
                type: 'select'
            });
        }
    })
    //回车键提交
    $('#tag_search_form').keydown(function(e){
        var e = e || event,
            keycode = e.which || e.keyCode;
        if(keycode == 13){
            $(".sub").trigger('click');
            return false;
        }
    })
    $('body').on('click','#recommend', function (){
        var this_val = $(this).text();
        var val = 0;
        $(this).parent().find('select.input-sm option').each(function(){
            if(this_val == $(this).text()){
                val = $(this).attr('value');
                return false;
            }
        });
        $(this).parent().find('select.input-sm option[value="'+val+'"]').attr('selected',true);
    })
</script>
{/block}